#!/bin/bash
#
# master-backup 脚本 2016-08-25 
#
time_hour=$(date +%Y%m%d%k)
backup_dir=/var/nas/backup_master/$time_hour
test ! -d "$backup_dir" && mkdir -p $backup_dir
kubeCategorys="pv pvc rc svc"
for kubeCategory in $kubeCategorys
do
        #echo "kubeCategory: $kubeCategory" 
        kubeItems=`kubectl get $kubeCategory|grep -v 'NAME'|awk '{print $1}'`
        echo "kubeItems: $kubeItems"
        for kubeItem in $kubeItems
        do
                kubectl --v=9 get $kubeCategory $kubeItem > /dev/null 2> $backup_dir/$kubeItem-$kubeCategory.json
        done
done
ls $backup_dir

sed -i 's/Response Body: /\n/g' $backup_dir/*
cd $backup_dir
for file in `ls *.json`;do
	echo $file ;
	total=`wc -l $file|awk '{print $1}'`;
	deleteline=$(expr $total - 1);
	if [[ $deleteline -gt 1 ]]; then
		sed -i "1,$deleteline d" $file;
	else
		echo "ERROR!!";
		cat $file;
	fi
	wc -l $file;
done
